/**
 * \file
 *           tlmServiceMsg.cpp
 * \brief
 *           Implements tlmServiceMsg class for Telematics
 *           this is the class for service message
 *
 * \par Author (last changes):
 *          - Zeng Jie
 *          - BUIC, Continental Automotive Holdings Co., Ltd.
 * \par Project Leader:
 *          -
 * \par Responsible Developer:
 *          - Zeng Jie
 *          - BUIC, Continental Automotive Holdings Co., Ltd.
 * \par Responsible Architect:
 *          - Zhang Fa
 *          - BUIC, Continental Automotive Holdings Co., Ltd.
 *
 * \par Project:
 *      DPCA2
 *
 * \par SW-Component:
 *      Telematics
 *
 * \par SW-Package:
 *      Telematics
 *
 * \par SW-Module:
 *      Telematics Application
 *
 * \par SW-Architecture Specification:
 *      DPCA –TELEMATICS SW DD
 *      05b
 *
 * \par SW-Design Specification:
 *      DPCA –TELEMATICS SW DD
 *      05b
 *
 * \par SW-Test Case Specification:
 *
 * \par CM-Info:
 *
 * \note
 *      This is a note.
 *
 * \bug
 *      - there are no known bugs
 *
 * \par Module-History:
 *
 * \verbatim
 * Date                Author					Reason
 * 10 Dec 2013        Zeng Jie              Initial Creation
 * The latest entry at the head of the history list.
 * \endverbatim
 *
 * \par Copyright Notice:
 * Copyright (C) 2012 Continental Automotive Holdings Co., Ltd.
 * Alle Rechte vorbehalten. All Rights Reserved.\n
 * The reproduction, transmission or use of this document or its contents is
 * not permitted without express written authority.\n
 * Offenders will be liable for damages. All rights, including rights created
 * by patent grant or registration of a utility model or design, are reserved.
 *
 */
#include "stdafx.h"
#include "Telematics.h"
#include "string.h"
#include "tlmServiceFactory.h"
#include "tlmServiceMsg.h"
#include <windows.h>


//## attribute mDataInfo
#include "tlmRmtCfgDataInfo.h"
//## operation GetParaAdmin()
#include "tlmRmtCfgParaAdmin.h"
//## operation GetParaNavi()
#include "tlmRmtCfgParaNavi.h"
//## operation GetParaWeb()
#include "tlmRmtCfgParaWeb.h"
//## attribute mSaver
#include "tlmRmtCfgSaver.h"
//## attribute mItsService
#include "tlmServiceApp.h"

/**
* \brief tlmServiceMsg: this is default constructor function of tlmServiceMsg
*
* \note
*
* \param[in]
* - none
*
* \return
* - none
*
*/
tlmServiceMsg::tlmServiceMsg() : mProtocolVersion(E_TELE_REMOTE_CONFIG_NGTP_HEADER_PROTOCOL_VERSION), mVin(E_TELE_REMOTE_CONFIG_NGTP_HEADER_VIN, TLM_RMT_CFG_HEADER_VIN_VALUE_SIZE), mRouteInfo(E_TELE_REMOTE_CONFIG_NGTP_HEADER_ROUTE_INFO,TLM_RMT_CFG_HEADER_TWO_BYTE_VALUE_SIZE), mMsgUniqueId(E_TELE_REMOTE_CONFIG_NGTP_HEADER_MSG_UNIQUE_ID,TLM_RMT_CFG_HEADER_TWO_BYTE_VALUE_SIZE), mSrvType(E_TELE_REMOTE_CONFIG_NGTP_HEADER_SERVICE_TYPE), mMsgType(E_TELE_REMOTE_CONFIG_NGTP_HEADER_MESSAGE_TYPE), mDateTime(E_TELE_REMOTE_CONFIG_NGTP_HEADER_DATE_TIME,TLM_RMT_CFG_HEADER_DATE_TIME_VALUE_SIZE), mMsgProtocolVersion(E_TELE_REMOTE_CONFIG_NGTP_HEADER_MSG_PROTOCOL_VERSION), mDataLength(E_TELE_REMOTE_CONFIG_NGTP_HEADER_DATA_LENGTH,TLM_RMT_CFG_HEADER_THREE_BYTE_VALUE_SIZE),mInboundMsgBuf(),mOutboundMsgBuf(), mInMsgPtr(NIL), mOutMsgPtr(NIL), mTotalSize(0), mDataInfoMsgPtr(NIL), mSaver(NIL), mDataInfo(NIL), mItsService(NIL) {
}

/**
* \brief tlmServiceMsg: this is constructor function of tlmServiceMsg
*                   it use message string and size to init this object
*                   it's always used on received message
*
* \note
*
* \param[in]
* - msgString: message buffer pointer
*   size: size of the message
*
* \return
* - none
*
*/
tlmServiceMsg::tlmServiceMsg(const uint8_t* msgString, const uint32_t size) : mProtocolVersion(E_TELE_REMOTE_CONFIG_NGTP_HEADER_PROTOCOL_VERSION), mVin(E_TELE_REMOTE_CONFIG_NGTP_HEADER_VIN, TLM_RMT_CFG_HEADER_VIN_VALUE_SIZE), mRouteInfo(E_TELE_REMOTE_CONFIG_NGTP_HEADER_ROUTE_INFO,TLM_RMT_CFG_HEADER_TWO_BYTE_VALUE_SIZE), mMsgUniqueId(E_TELE_REMOTE_CONFIG_NGTP_HEADER_MSG_UNIQUE_ID,TLM_RMT_CFG_HEADER_TWO_BYTE_VALUE_SIZE), mSrvType(E_TELE_REMOTE_CONFIG_NGTP_HEADER_SERVICE_TYPE), mMsgType(E_TELE_REMOTE_CONFIG_NGTP_HEADER_MESSAGE_TYPE), mDateTime(E_TELE_REMOTE_CONFIG_NGTP_HEADER_DATE_TIME,TLM_RMT_CFG_HEADER_DATE_TIME_VALUE_SIZE), mMsgProtocolVersion(E_TELE_REMOTE_CONFIG_NGTP_HEADER_MSG_PROTOCOL_VERSION), mDataLength(E_TELE_REMOTE_CONFIG_NGTP_HEADER_DATA_LENGTH,TLM_RMT_CFG_HEADER_THREE_BYTE_VALUE_SIZE), mInboundMsgBuf(), mOutboundMsgBuf(), mInMsgPtr(NIL), mOutMsgPtr(NIL), mTotalSize(size), mDataInfoMsgPtr(NIL), mSaver(NIL), mDataInfo(NIL), mItsService(NIL) {
    //#[ operation tlmServiceMsg(uint8_t*,uint32_t)
    mInMsgPtr = new unsigned char[size];
    memcpy(mInMsgPtr, msgString, size);
    
    //mTotalSize=size; 
    
    mInboundMsgBuf.setMsgBufferPtr(mInMsgPtr);
    mInboundMsgBuf.setMsgBufferSize(TLM_RMT_CFG_NGTP_HEADER_LENGTH);
    
    //#]
}

/**
* \brief tlmServiceMsg: this is constructor function of tlmServiceMsg
*                   it use srvType and msgType to init object
*                   it's always used on send out message
*
* \note
*
* \param[in]
* - srvType: service type
*   msgType: message type
*
* \return
* - none
*
*/
tlmServiceMsg::tlmServiceMsg(uint8_t srvType, uint8_t msgType) : mProtocolVersion(E_TELE_REMOTE_CONFIG_NGTP_HEADER_PROTOCOL_VERSION), mVin(E_TELE_REMOTE_CONFIG_NGTP_HEADER_VIN, TLM_RMT_CFG_HEADER_VIN_VALUE_SIZE), mRouteInfo(E_TELE_REMOTE_CONFIG_NGTP_HEADER_ROUTE_INFO,TLM_RMT_CFG_HEADER_TWO_BYTE_VALUE_SIZE), mMsgUniqueId(E_TELE_REMOTE_CONFIG_NGTP_HEADER_MSG_UNIQUE_ID,TLM_RMT_CFG_HEADER_TWO_BYTE_VALUE_SIZE), mSrvType(E_TELE_REMOTE_CONFIG_NGTP_HEADER_SERVICE_TYPE), mMsgType(E_TELE_REMOTE_CONFIG_NGTP_HEADER_MESSAGE_TYPE), mDateTime(E_TELE_REMOTE_CONFIG_NGTP_HEADER_DATE_TIME,TLM_RMT_CFG_HEADER_DATE_TIME_VALUE_SIZE), mMsgProtocolVersion(E_TELE_REMOTE_CONFIG_NGTP_HEADER_MSG_PROTOCOL_VERSION), mDataLength(E_TELE_REMOTE_CONFIG_NGTP_HEADER_DATA_LENGTH,TLM_RMT_CFG_HEADER_THREE_BYTE_VALUE_SIZE),mInboundMsgBuf(), mOutboundMsgBuf(), mInMsgPtr(NIL), mOutMsgPtr(NIL), mTotalSize(0), mDataInfoMsgPtr(NIL), mSaver(NIL), mDataInfo(NIL), mItsService(NIL) {
    //#[ operation tlmServiceMsg(uint8_t,uint8_t)
    //the default message only have header.
    uint32_t size=TLM_RMT_CFG_NGTP_HEADER_LENGTH;  
    uint32_t maxDataInfoSize=0;
    
    SetSrvType(srvType);
    SetMsgType(msgType);  
    
    mDataInfo = tlmServiceFactory::GetInstance()->CreateDataInfo(msgType);   
    
    if(mDataInfo!=NIL)
    {//this message have data info, we should get its max size
    	maxDataInfoSize=mDataInfo->GetMaxDataInfoSize(); 
    	size += maxDataInfoSize;
    } 
    //allocate memory for max size.
    mOutMsgPtr = new unsigned char[size]; 
    if(mOutMsgPtr!=NIL)
    {
    	memset(mOutMsgPtr, 0, size);  
    	mOutboundMsgBuf.setMsgBufferPtr(mOutMsgPtr);                       
    	//we only process the header, the data info is process in mDataInfo object.
    	mOutboundMsgBuf.setMsgBufferSize(TLM_RMT_CFG_NGTP_HEADER_LENGTH);  
    	
    	if(mDataInfo!=NIL)
    	{
    		mDataInfoMsgPtr=mOutMsgPtr+TLM_RMT_CFG_NGTP_HEADER_LENGTH;
    	   //here we set the mDataInfo buffer,so it can be used when Fill.
    		mDataInfo->SetDataInfoBuf(mOutMsgPtr+TLM_RMT_CFG_NGTP_HEADER_LENGTH,maxDataInfoSize);
    	}
    }
    
    //#]
}

/**
* \brief ~tlmServiceMsg: this is destructor of tlmServiceMsg class
*                   it deletes all allocated memory in the constructor function
*
* \note
*
* \param[in]
* - none
*
* \return
* - none
*/
tlmServiceMsg::~tlmServiceMsg() {
    //#[ operation ~tlmServiceMsg()
    if(mInMsgPtr!=NIL)
    {
    	delete [] mInMsgPtr; 
    	mInMsgPtr=NIL;
    	//mInboundMsgBuf.setMsgBufferSize(0);  
    	//mInboundMsgBuf.reset();
    }    
    
    if(mOutMsgPtr!=NIL)
    {
    	delete [] mOutMsgPtr; 
    	mOutMsgPtr=NIL; 
    	//mOutboundMsgBuf.setMsgBufferSize(0); 
    	//mOutboundMsgBuf.reset();
    }
    
    if(mDataInfo != NIL)
    {
    	delete mDataInfo;
    	mDataInfo=NIL;
    }
    //#]
}



/**
* \brief GetData: get data content from the message buffer 
*
* \note
*
* \param[in]
* - buf:buffer to save the read data 
*   size: size of the data that want to be read
*
* \return
* - BOOL: TRUE - get data successfully.
*         FALSE - get data failed
*
* DesignID: MRN_TLM-SRV_SW_DD_065
*/
BOOL tlmServiceMsg::GetData(uint8_t* buf, uint32_t size) {
    //#[ operation GetData(uint8_t*,uint32_t)
    //#]
	return mInboundMsgBuf.getData(buf, size);

}

/**
* \brief SetData: set data content into the message buffer 
*
* \note
*
* \param[in]
* - buf:buffer of the data that shall be set
*   size: size of the data that want to be set
*
* \return
* - BOOL: TRUE - set data successfully.
*         FALSE - set data failed
*
* DesignID: MRN_TLM-SRV_SW_DD_066
*/
BOOL tlmServiceMsg::SetData(uint8_t * buf, uint32_t size) {
    //#[ operation SetData(uint8_t *,uint32_t)
    return mOutboundMsgBuf.setData(buf, size);
    //#]
}

/**
* \brief SetValue: set one byte data content into the message buffer 
*
* \note
*
* \param[in]
* - value: value of the data that shall be set
*
* \return
* - BOOL: TRUE - set value successfully.
*         FALSE - set value failed
*
* DesignID: MRN_TLM-SRV_SW_DD_067
*/
BOOL tlmServiceMsg::SetValue(uint8_t value) {
    //#[ operation SetValue(uint8_t)
    return mOutboundMsgBuf.setValue(value);
    //#]
}
/**
* \brief ParseInit: parse the message and set its attribute
*
* \note
*
* \param[in]
* - none
*
* \return
* - BOOL: TRUE - parse message successfully.
*         FALSE - parse message failed
*
* DesignID: MRN_TLM-SRV_SW_DD_068
*/
BOOL tlmServiceMsg::ParseInit() {
    //#[ operation ParseInit()
    uint8_t value;
    tlmRmtCfgElement* pItem=NIL;
    BOOL retVal = TRUE;
    
    //when the object is created, it can only handle the header, as the length is NGTP_HEADER_LENGTH
    //this length does not include sep byte 0x7E that seperate the header and data info.
    
    //the first byte should header item code
	while(!mInboundMsgBuf.IsReachEnd())
    {//first get the code  
    
    	retVal=GetValue(value); 
    	if(retVal)
    	{
    		pItem = GetHeaderItem(value);
    	    if(pItem != NIL)
    	    {//init the header item
    	        retVal = pItem->ParseInit(*this);
    	    }
    	}    
        
        if(retVal&&(!mInboundMsgBuf.IsReachEnd()))
        {//after the element value, it should be sep "0x7E"    
        	retVal = GetValue(value);
            if(value != SEPERATE_LEVEL_1)
            {//invalid sepeartor, no need to check the message
    	        LOG_PRINT(DPCA_TLM_RMT_CFG_ERROR,"[TLMRMTCFGMENU]: <tlmRmtCfgSrvMsg::ParseInit>invalide Seperator[%d], no need to parse the message\r\n",value);
                retVal = FALSE;
                break;
            }
        }
    }  
    
    if(retVal)
    {
    	//after parse the header, it should be data info buf
    	mDataInfoMsgPtr = mInboundMsgBuf.getCurPtr();
    	
    	uint8_t srvType=0;
    	retVal = GetSrvType(srvType);
    	LOG_PRINT(DPCA_TLM_RMT_CFG_ERROR,"[TLMRMTCFGMENU]: <tlmRmtCfgSrvMsg::ParseInit> srvType[0x%x]\r\n",srvType);
    	
    	if(srvType == TLM_NGTP_HEADER_ITEM_SRV_TYPE_INCOMING_TRAFFIC)
    	{//this is incoming traffic message, it should save the message
    	    mSaver =  tlmServiceFactory::GetInstance()->GetSaverInstance();
    	    if(mSaver == NIL)
    	    {
    	        LOG_PRINT(DPCA_TLM_RMT_CFG_ERROR,"[TLMRMTCFGMENU]: <tlmRmtCfgSrvMsg::ParseInit>create saver failed\r\n");
    	    }
    	}
    }
    
    return retVal;
    
    
    //#]
}

/**
* \brief GetValue: get one byte data value from the message buffer 
*
* \note
*
* \param[in]
* - value: variable to save the read data value
*
* \return
* - BOOL: TRUE - get value successfully.
*         FALSE - get value failed
*
* DesignID: MRN_TLM-SRV_SW_DD_069
*/
BOOL tlmServiceMsg::GetValue(uint8_t& value) {
    //#[ operation GetValue(uint8_t&)
    return mInboundMsgBuf.getValue(value);
    //#]
}



/**
* \brief GetHeaderItem: get the message header element by code 
*
* \note
*
* \param[in]
* - code: code of the header element
*
* \return
* - tlmRmtCfgElement* : pointer to header element
*
* DesignID: MRN_TLM-SRV_SW_DD_070
*/
tlmRmtCfgElement* tlmServiceMsg::GetHeaderItem(const uint8_t& code) {
    //#[ operation GetHeaderItem(const uint8_t& code)
    tlmRmtCfgElement* pItem=NIL;
    
    switch(code)
    {
        case E_TELE_REMOTE_CONFIG_NGTP_HEADER_PROTOCOL_VERSION:
        {
            pItem = &mProtocolVersion;
            break;
        }
        case E_TELE_REMOTE_CONFIG_NGTP_HEADER_VIN:
        {
            pItem = &mVin;
            break;
        }
        case E_TELE_REMOTE_CONFIG_NGTP_HEADER_ROUTE_INFO:
        {
            pItem = &mRouteInfo;
            break;
        }
        case E_TELE_REMOTE_CONFIG_NGTP_HEADER_MSG_UNIQUE_ID:
        {
            pItem = &mMsgUniqueId;
            break;
        }
        case E_TELE_REMOTE_CONFIG_NGTP_HEADER_SERVICE_TYPE:
        {
            pItem = &mSrvType;
            break;
        }
        case E_TELE_REMOTE_CONFIG_NGTP_HEADER_MESSAGE_TYPE:
        {
            pItem = &mMsgType;
            break;
        }
        case E_TELE_REMOTE_CONFIG_NGTP_HEADER_DATE_TIME:
        {
            pItem = &mDateTime;
            break;
        }
        case E_TELE_REMOTE_CONFIG_NGTP_HEADER_MSG_PROTOCOL_VERSION:
        {
            pItem = &mMsgProtocolVersion;
            break;
        }
        case E_TELE_REMOTE_CONFIG_NGTP_HEADER_DATA_LENGTH:
        {
            pItem = &mDataLength;
            break;
        }
        default:
        {
            pItem = NIL;
    	    LOG_PRINT(DPCA_TLM_RMT_CFG_ERROR,"[TLMRMTCFGMENU]: <teleSrvMsg::GetItem>invalide element code[%d]\r\n",code);
            break;
        }
    }
    
    return pItem;
    //#]
}

/**
* \brief GetSrvType: get servie type
*
* \note
*
* \param[in]
* - srvType: variable that store the service type
*
* \return
* - BOOL: TRUE - get service type successfully.
*         FALSE - get service type failed
*
* DesignID: MRN_TLM-SRV_SW_DD_071
*/
BOOL tlmServiceMsg::GetSrvType(uint8_t & srvType) const{
    //#[ operation GetSrvType(uint8_t &)
    uint8_t * p=NIL;
    BOOL retVal = TRUE;
    
    p=mSrvType.getMValuePtr();
    if(p==NIL)
    {
        LOG_PRINT(DPCA_TLM_RMT_CFG_ERROR,"[TLMRMTCFGMENU]: <tlmRmtCfgSrvMsg::GetSrvType> value ptr for srvtype is NIL\r\n");
        retVal=FALSE;
    }
    else
    {
        srvType = *p;
    }
    
    return retVal;
    
    //#]
}

/**
* \brief SetSrvType: set servie type
*
* \note
*
* \param[in]
* - srvType: service type that shall be set
*
* \return
* - BOOL: TRUE - set service type successfully.
*         FALSE - set service type failed
*
* DesignID: MRN_TLM-SRV_SW_DD_072
*/
BOOL tlmServiceMsg::SetSrvType(uint8_t & srvType) {
    //#[ operation SetSrvType(const uint8_t &)
    
    BOOL retVal = TRUE;
    
    mSrvType.SetValue(&srvType);
    
    
    return retVal;
    
    //#]
}

/**
* \brief GetMsgType: get message type
*
* \note
*
* \param[in]
* - msgType: variable that store the message type
*
* \return
* - BOOL: TRUE - get message type successfully.
*         FALSE - get message type failed
*
* DesignID: MRN_TLM-SRV_SW_DD_073
*/
BOOL tlmServiceMsg::GetMsgType(uint8_t & msgType) const{
    //#[ operation GetMsgType(uint8_t &)
    uint8_t * p=NIL;
    BOOL retVal = TRUE;
    
    p=mMsgType.getMValuePtr();
    if(p==NIL)
    {
        LOG_PRINT(DPCA_TLM_RMT_CFG_ERROR,"[TLMRMTCFGMENU]: <tlmRmtCfgSrvMsg::GetMsgType> value ptr for msgtype is NIL\r\n");
        retVal=FALSE;
    }
    else
    {
        msgType = *p;
    }
    
    return retVal;
    
    //#]
}

/**
* \brief SetMsgType: set message type
*
* \note
*
* \param[in]
* - msgType: message type that shall be set
*
* \return
* - BOOL: TRUE - set message type successfully.
*         FALSE - set message type failed
*
* DesignID: MRN_TLM-SRV_SW_DD_074
*/
BOOL tlmServiceMsg::SetMsgType(uint8_t & msgType) {
    //#[ operation SetMsgType(uint8_t &)
    
    BOOL retVal = TRUE;
    
    mMsgType.SetValue(&msgType);
    
    
    return retVal;
    
    //#]
}

/**
* \brief SaveMsg: save the message buffer
*
* \note
*
* \param[in]
* - none
*
* \return
* - BOOL: TRUE - save message successfully.
*         FALSE - save message failed
*
* DesignID: MRN_TLM-SRV_SW_DD_075
*/
BOOL tlmServiceMsg::SaveMsg() const{
    //#[ operation SaveMsg()
    BOOL retVal=TRUE;
    
    if(mSaver!=NIL)
    {
        retVal = mSaver->SaveMsg(mInMsgPtr, mTotalSize);
    }
    
    return retVal;
    
    //#]
}

/**
* \brief GetDataInfoLength: get data info length which is in the length section of the message
*
* \note
*
* \param[in]
* - dataInfoLen: variable to save the data info length
*
* \return
* - BOOL: TRUE - get data info length successfully
*         FALSE - get data info length failed
*
* DesignID: MRN_TLM-SRV_SW_DD_076
*/
BOOL tlmServiceMsg::GetDataInfoLength(uint32_t& dataInfoLen) const{
    //#[ operation GetDataInfoLength(uint32_t&)
    uint8_t * p=NIL;
    BOOL retVal = TRUE;
    
    p=mDataLength.GetValuePtr();
    if(p==NIL)
    {
        LOG_PRINT(DPCA_TLM_RMT_CFG_ERROR,"[TLMRMTCFGMENU]: <tlmRmtCfgSrvMsg::GetDataInfoLength> value ptr for datainfolen is NIL\r\n");
        dataInfoLen=0;
        retVal=FALSE;
    }
    else
    {
        dataInfoLen = (*p)<<16 | (*(p+1))<<8 | (*(p+2));
    }
    
    return retVal;
    
    //#]
}

/**
* \brief SetDataInfoLength: set data info length 
*
* \note
*
* \param[in]
* - dataInfoLen: data info length that shall be set
*
* \return
* - BOOL: TRUE - set data info length successfully
*         FALSE - set data info length failed
*
* DesignID: MRN_TLM-SRV_SW_DD_077
*/
BOOL tlmServiceMsg::SetDataInfoLength(const uint32_t& dataInfoLen) {
    //#[ operation SetDataInfoLength(const uint32_t&)
    BOOL retVal = TRUE;   
    uint8_t tmpVal[3]={0};   
    
    tmpVal[0]= (dataInfoLen & 0xff0000) >> 16;
    tmpVal[1]= (dataInfoLen & 0xff00) >> 8; 
    tmpVal[2]= (dataInfoLen & 0xff);
    
    mDataLength.SetValue(tmpVal);
                
    return retVal;
    
    
    
    
    //#]
}

/**
* \brief HandleData: this function shall parse the datainfo section
*
* \note
*
* \param[in]
* - none
*
* \return
* - BOOL: TRUE - parse data info section successfully
*         FALSE - parse data info section failed
*
* DesignID: MRN_TLM-SRV_SW_DD_078
*/
BOOL tlmServiceMsg::HandleData() {
    //#[ operation HandleData()
    uint32_t dataInfoLen=0;
    uint8_t msgType=0;  
    BOOL retVal=TRUE;
    
    GetDataInfoLength(dataInfoLen);
    LOG_PRINT(DPCA_TLM_RMT_CFG_ERROR,"[TLMRMTCFGMENU]: <tlmRmtCfgSrvMsg::HandleData> data info length[%d]\r\n",dataInfoLen);
    
    if(dataInfoLen != 0)
    {
        retVal = GetMsgType(msgType);
        LOG_PRINT(DPCA_TLM_RMT_CFG_ERROR,"[TLMRMTCFGMENU]: <tlmRmtCfgSrvMsg::HandleData> msgType[0x%x]\r\n",msgType);
        
        mDataInfo = tlmServiceFactory::GetInstance()->CreateDataInfo(msgType, dataInfoLen, mDataInfoMsgPtr);
        if(mDataInfo==NIL)
        {
            LOG_PRINT(DPCA_TLM_RMT_CFG_ERROR,"[TLMRMTCFGMENU]: <tlmRmtCfgSrvMsg::HandleData> create data info instance failed\r\n");
            retVal = FALSE;
        }
        else
        {
            retVal = mDataInfo->ParseInit();
        }
        
    }        
    
    return retVal;
    
    //#]
}

/**
* \brief SetDataInfoMsgPtr: set pointer which indicates the buffer of data info
*
* \note
*
* \param[in]
* - ptr: pointer to data info
*
* \return
* - none
*
* DesignID: MRN_TLM-SRV_SW_DD_079
*/
void tlmServiceMsg::SetDataInfoMsgPtr(uint8_t* ptr) {
    //#[ operation SetDataInfoMsgPtr(uint8_t*)
    mDataInfoMsgPtr = ptr;
    //#]
}

/**
* \brief Init: this function is used to init message object's attribute 
*
* \note
*
* \param[in]
* - none
*
* \return
* - BOOL: TRUE - init message attribute successfully
*         FALSE - init message attribute failed
*
* DesignID: MRN_TLM-SRV_SW_DD_080
*/
BOOL tlmServiceMsg::Init() {
    //#[ operation Init()
    //Using this function when sending the message.
    //The msgType and srvType has been set when create this outing message.
    
    BOOL retVal = TRUE;
    uint8_t proVersion=1;
    uint8_t vin[TLM_RMT_CFG_HEADER_VIN_VALUE_SIZE+1]={0};
    uint16_t routInfo=0;
    uint16_t uniqueId=0;
    uint32_t dateTime=0;
    uint8_t msgProVersion=1;
    uint32_t dataInfoLen=0; 
    
    //TODO:need get current settings.
    //
    //use test vin code;
	memcpy(vin,"TESTVIN123", TLM_RMT_CFG_HEADER_VIN_VALUE_SIZE);

    SetHdrProtocolVersion(proVersion);
    SetHdrVin(vin);
    SetHdrRoutInfo(routInfo);
    SetHdrUniqueId(uniqueId);
	//TODO:implement DateTime
    SetHdrDateTime(dateTime);
    SetHdrMsgProVersion(msgProVersion);   
    
    //do not set dataInfoLen, as we don't the actual data info length until we fill the buffer.
    
    if(mDataInfo != NIL)
    {
        //the service pointed by mpService have the current configuration.
        mDataInfo->SetItsService(mItsService);
        mDataInfo->Init();
    }
    
    
    return retVal;
    
    //#]
}

/**
* \brief SetHdrProtocolVersion: set protocol version of the header section
*
* \note
*
* \param[in]
* - proVer: protocol version
*
* \return
* - BOOL: TRUE - set protocol version successfully
*         FALSE - set protocol version failed
*
* DesignID: MRN_TLM-SRV_SW_DD_081
*/
BOOL tlmServiceMsg::SetHdrProtocolVersion(uint8_t & proVer) {
    //#[ operation SetHdrProtocolVersion(uint8_t &)
    
    BOOL retVal = TRUE;
    
    mProtocolVersion.SetValue(&proVer);
    
    
    return retVal;
    
    //#]
}

/**
* \brief SetHdrVin: set vin of the header section
*
* \note
*
* \param[in]
* - vin: vin code
*
* \return
* - BOOL: TRUE - set vin successfully
*         FALSE - set vin failed
*
* DesignID: MRN_TLM-SRV_SW_DD_082
*/
BOOL tlmServiceMsg::SetHdrVin(uint8_t * vin) {
    //#[ operation SetHdrVin(uint8_t *)
    
    BOOL retVal = TRUE;
    
    mVin.SetValue(vin);
    
    
    return retVal;
    
    //#]
}

/**
* \brief SetHdrRoutInfo: set route info of the header section
*
* \note
*
* \param[in]
* - routeInfo: route info
*
* \return
* - BOOL: TRUE - set route Info successfully
*         FALSE - set route Info failed
*
* DesignID: MRN_TLM-SRV_SW_DD_083
*/
BOOL tlmServiceMsg::SetHdrRoutInfo(uint16_t & routeInfo) {
    //#[ operation SetHdrRoutInfo(uint16_t &)
    
    BOOL retVal = TRUE;      
    
    uint8_t tmpVal[2]={0};   

	tmpVal[0]= (routeInfo & 0xff00) >> 8; 
	tmpVal[1]= (routeInfo & 0xff);

	mRouteInfo.SetValue(tmpVal);  
                
    return retVal;
    
    //#]
}

/**
* \brief SetHdrUniqueId: set unique id of the header section
*
* \note
*
* \param[in]
* - msgUniqueId: unique id
*
* \return
* - BOOL: TRUE - set unique id successfully
*         FALSE - set unique id failed
*
* DesignID: MRN_TLM-SRV_SW_DD_084
*/
BOOL tlmServiceMsg::SetHdrUniqueId(uint16_t & msgUniqueId) {
    //#[ operation SetHdrUniqueId(uint16_t &)
    
    BOOL retVal = TRUE;
    
    
    //should consider the outing buffer is big-endian.
    uint8_t tmpVal[2]={0};   

	tmpVal[0]= (msgUniqueId & 0xff00) >> 8; 
	tmpVal[1]= (msgUniqueId & 0xff);

	mMsgUniqueId.SetValue(tmpVal);
    
    
    
    return retVal;
    
    //#]
}

/**
* \brief TranslateDateTime: Translate system time to MRN date time format
*
* \note
*
* \param[in]
* - dateTime: date time buffer
*
* \return
* - none
*
* DesignID: MRN_TLM-SRV_SW_DD_095
*/
void tlmServiceMsg::TranslateDateTime(uint8_t * dateTime)
{
	SYSTEMTIME st;
	//we get systeme time at here
	GetSystemTime(&st);
	

	dateTime[0] = ((st.wYear & 0x3f) << 2) | ((st.wMonth & 0x0f)>>2);
	dateTime[1] = ((st.wMonth & 0x0f) << 6) | ((st.wDay & 0x1f)<<1) | ((st.wHour & 0x1f)>>4);
	dateTime[2] = ((st.wHour & 0x1f)<<4) | ((st.wMinute & 0x3f)>>2);
	dateTime[3] = ((st.wMinute & 0x3f)<<6)| (st.wSecond & 0x3f);
}

/**
* \brief SetHdrDateTime: set date time of the header section
*
* \note
*
* \param[in]
* - dateTime: date time
*
* \return
* - BOOL: TRUE - set date time successfully
*         FALSE - set date time failed
*
* DesignID: MRN_TLM-SRV_SW_DD_085
*/
BOOL tlmServiceMsg::SetHdrDateTime(uint32_t & dateTime) {
    //#[ operation SetHdrDateTime(uint32_t &)
    
    BOOL retVal = TRUE;

	
    
    //TODO:implemente date time.
	mDateTime.SetValue(reinterpret_cast<uint8_t*>(&dateTime));

    
    
    return retVal;
    
    //#]
}

/**
* \brief SetHdrMsgProVersion: set message protocol version of the header section
*
* \note
*
* \param[in]
* - msgProVer: message protocol version
*
* \return
* - BOOL: TRUE - set message protocol version successfully
*         FALSE - set message protocol version failed
*
* DesignID: MRN_TLM-SRV_SW_DD_086
*/
BOOL tlmServiceMsg::SetHdrMsgProVersion(uint8_t & msgProVer) {
    //#[ operation SetHdrMsgProVersion(uint8_t &)
    
    BOOL retVal = TRUE;
    
    mMsgProtocolVersion.SetValue(&msgProVer);
    
    
    return retVal;
    
    //#]
}

/**
* \brief Fill: fill the message buffer by using its attribute
*
* \note
*
* \param[in]
* - none
*
* \return
* - BOOL: TRUE - fill the message buffer successfully
*         FALSE - fill the message buffer failed
*
* DesignID: MRN_TLM-SRV_SW_DD_087
*/
BOOL tlmServiceMsg::Fill() {
    //#[ operation Fill()
    BOOL retVal=TRUE; 
    uint32_t dataInfoSize=0;
    
    //As we don't know actual size of data info until fill it
    //we first fill the data info ,get the actual length, then fill the header
    if(mDataInfo != NIL)
    {//This message has data info, we should fill it, the buffer has been set when create this msg object.  
    	mDataInfo->Fill(); 
    	
    	//after fill, we know the actual datainfo size.
    	dataInfoSize=mDataInfo->GetDataInfoSize();
    } 
    
    SetDataInfoLength(dataInfoSize);
    
    retVal = FillHeader(); 

	mTotalSize=dataInfoSize+TLM_RMT_CFG_NGTP_HEADER_LENGTH;
    
    return retVal;
    //#]
}

/**
* \brief Fill: fill the header section of message buffer by using its attribute
*
* \note
*
* \param[in]
* - none
*
* \return
* - BOOL: TRUE - fill the message buffer header successfully
*         FALSE - fill the message buffer header failed
*
* DesignID: MRN_TLM-SRV_SW_DD_088
*/
BOOL tlmServiceMsg::FillHeader() {
    //#[ operation FillHeader()
    BOOL retVal=TRUE;
    
    tlmRmtCfgElement * pItem = NIL;
    
    //fill protocol version
    mProtocolVersion.Fill(*this);
    
    SetValue(SEPERATE_LEVEL_1);
    
    //fill vin
    mVin.Fill(*this);
    
    SetValue(SEPERATE_LEVEL_1);
    
    //fill routInfo
    mRouteInfo.Fill(*this);
    
    SetValue(SEPERATE_LEVEL_1);
    
    //fill unique ID
    mMsgUniqueId.Fill(*this);
    
    SetValue(SEPERATE_LEVEL_1);
    
    //fill srvType
    mSrvType.Fill(*this);
    
    SetValue(SEPERATE_LEVEL_1);
    
    //fill msgType
    mMsgType.Fill(*this);
    
    SetValue(SEPERATE_LEVEL_1);
    
    //fill datatime
    mDateTime.Fill(*this);
    
    SetValue(SEPERATE_LEVEL_1);
    
    //fill msg protocol version
    mMsgProtocolVersion.Fill(*this);

	SetValue(SEPERATE_LEVEL_1);
    
    //fill data info length
    mDataLength.Fill(*this);
    
    //here does not fill the seperator as it will be set in data info
    
        
    
    return retVal;
    
    
    
    //#]
}

/**
* \brief GetTotalSize: get total size of the message
*
* \note
*
* \param[in]
* -none 
*
* \return
* - uint32_t : total size of the message
*
* DesignID: MRN_TLM-SRV_SW_DD_089
*/
uint32_t tlmServiceMsg::GetTotalSize() const{
	return mTotalSize;
}

/**
* \brief GetParaWeb : get the Web parameter object
*
* \note
*
* \param[in]
* - none
*
* \return
* - tlmRmtCfgParaWeb* : pointer to Web parameter object
*
* DesignID: MRN_TLM-SRV_SW_DD_090
*/
tlmRmtCfgParaWeb* tlmServiceMsg::GetParaWeb() {
    //#[ operation GetParaWeb()
    tlmRmtCfgParaWeb *pParaWeb=NIL;
    if(mDataInfo!=NIL)
    {      
    	pParaWeb=static_cast<tlmRmtCfgParaWeb*>(mDataInfo->GetParaWeb());
    }
    
    return pParaWeb;
    //#]
}
/**
* \brief GetParaNavi : get the Navi parameter object
*
* \note
*
* \param[in]
* - none
*
* \return
* - tlmRmtCfgParaNavi* : pointer to Navi parameter object
*
* DesignID: MRN_TLM-SRV_SW_DD_091
*/
tlmRmtCfgParaNavi* tlmServiceMsg::GetParaNavi() {
	tlmRmtCfgParaNavi *pParaNavi=NIL;
	if(mDataInfo!=NIL)
	{      
		pParaNavi=static_cast<tlmRmtCfgParaNavi*>(mDataInfo->GetParaNavi());
	}

	return pParaNavi;
}
/**
* \brief GetParaAdmin : get the Admin parameter object
*
* \note
*
* \param[in]
* - none
*
* \return
* - tlmRmtCfgParaAdmin* : pointer to Admin parameter object
*
* DesignID: MRN_TLM-SRV_SW_DD_092
*/
tlmRmtCfgParaAdmin* tlmServiceMsg::GetParaAdmin() {
    tlmRmtCfgParaAdmin *pParaAdmin=NIL;
    if(mDataInfo!=NIL)
    {      
    	pParaAdmin=static_cast<tlmRmtCfgParaAdmin*>(mDataInfo->GetParaAdmin());
    }
    
    return pParaAdmin;
    //#]
}
/**
* \brief SetItsService: set the service pointer
*
* \note
*
* \param[in]
* - pSrv: pointer to service object
*
* \return
* - none
*
* DesignID: MRN_TLM-SRV_SW_DD_093
*/
void tlmServiceMsg::SetItsService(tlmServiceApp* pSrv) {
    
    //#[ operation SetItsService(tlmServiceApp)
    mItsService=pSrv;
    //#]
}

/**
* \brief GetOutMsgPtr: get the out message buffer pointer
*
* \note
*
* \param[in]
* - none
*
* \return
* - uint8_t*: pointer to the out message buffer
*
* DesignID: MRN_TLM-SRV_SW_DD_094
*/
uint8_t * tlmServiceMsg::GetOutMsgPtr() const{
    return mOutMsgPtr;
}
/**
* \brief getMInMsgPtr: get the in message buffer pointer
*
* \note
*
* \param[in]
* - none
*
* \return
* - uint8_t*: pointer to the in message buffer
*
*/
uint8_t* tlmServiceMsg::getMInMsgPtr() const {
    return mInMsgPtr;
}


/**
* \brief GetMOutMsgPtr: get the out message buffer pointer
*
* \note
*
* \param[in]
* - none
*
* \return
* - uint8_t*: pointer to the out message buffer
*
*/
uint8_t* tlmServiceMsg::getMOutMsgPtr() const {
    return mOutMsgPtr;
}


